<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 10/4/18
 * Time: 1:11 PM
 */

namespace home\modules\docu\models;


use common\models\db\DocuCommentReply;
use yii\base\Model;
use yii\db\Exception;

class FormCommentReply extends Model
{
    public $comment_id;
    public $parent_id;
    public $body;
    public $at_user;
    public $code;

    public function attributeLabels()
    {
        return [
            'comment_id' => "Comment Id",
            'parent_id' => "Parent Id",
            'body' => " Body",
            'at_user' => "At User",
            'code' => "Code",
        ];
    }

    public function rules()
    {
        return [
            [['comment_id', 'body', 'code'], 'required'],
            [['comment_id', 'parent_id', 'at_user'], 'number'],
            ['body', 'string', 'min' => 6, 'max' => 65535],
            ['code', 'captcha'],
        ];
    }

    /**
     * @throws Exception
     */
    public function entry()
    {
        $reply = new DocuCommentReply();
        $reply->docu_comment_id = $this->comment_id;
        $reply->body = $this->body;
        $reply->created_at = Yii_BT_TIME;
        $reply->created_by = \Yii::$app->user->id;
        $reply->status = DocuCommentReply::STATUS_DEFAULT;
        if ($this->parent_id)$reply->for_parent_id = $this->parent_id;
        if ($this->at_user)$reply->at_user = $this->at_user;
        if (!$reply->save()){
            throw new Exception(\common\members\wodrow\tools\Model::getModelError($reply));
        }
    }
}